' ***^^^***|||***^^^***
' 
'  This file is regenerated until it's edited. Delete the file to force regeneration.

'  NOTE: This denotes the way the file was created and it's possible the 
'        generation type has since changed.
'        Check the generation script for the curent state of the file.

' %%%###%%%c8bb265606cd367004950f866d1613cf%%%###%%%
' %%%##NoWS##%%%faa763cc2c5ab57d20f8782fae04728c%%%##NoWS##%%%
' ***^^^***|||***^^^***


'************************************************************************************
' Copyright © 2003-2006 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************

Option Explicit On
Option Strict On

Imports System
Imports System.Collections.Generic
Imports KadGen.MetadataWrapper
Imports KadGen.Utility2005.Tools


Public Class Dbs
   Inherits genDbs

   Shared Sub New()
      ' Ping the base class so constructors run tp set namespace
      Dim x As New Dbs()
   End Sub

   Public Overloads Shared Sub OutputWrapper( _
            ByVal xmlFileName As String, _
            ByVal MetadataRoot As DbsMetaDataRoot)
      Dim stream As IO.FileStream = Nothing
      Try
         stream = New IO.FileStream(xmlFileName, IO.FileMode.Create)
         OutputWrapper(stream, MetadataRoot)
      Finally
         If stream IsNot Nothing Then
            stream.Flush()
            stream.Close()
         End If
      End Try
   End Sub

   Public Overloads Shared Sub OutputWrapper( _
           ByVal stream As IO.Stream, _
            ByVal MetadataRoot As DbsMetaDataRoot)
      ' Open xml writer
      Dim xmlWriter As Xml.XmlWriter = Nothing
      Try
         xmlWriter = Xml.XmlWriter.Create(stream)
         xmlWriter.WriteStartDocument()
         xmlWriter.WriteStartElement(nsPrefix, "MetaDataRoot", ns)
         OutputWrapperPart(xmlWriter, MetadataRoot.DbsDataStructures)
         xmlWriter.WriteEndElement()
         xmlWriter.WriteEndDocument()
      Finally
         If xmlWriter IsNot Nothing Then
            xmlWriter.Flush()
         End If
      End Try
   End Sub

   ' We go through some contortions to have common code because it is likely to change
   Protected Overrides Function ShouldOutputTableColumnProperty( _
            ByVal propertyName As String, _
            ByVal DbsTableColumn As MetadataWrapper.DbsTableColumn) _
            As Boolean

      Dim isHandled As Boolean = False
      Dim value As Boolean = ShouldOutputTableOrViewColumnProperty(propertyName, DbsTableColumn.NETType, _
               DbsTableColumn.IsAutoIncrement.Value, DbsTableColumn.UseForDesc.Value, DbsTableColumn.Default, isHandled)
      If isHandled Then
         Return value
      Else
         Return MyBase.ShouldOutputTableColumnProperty(propertyName, DbsTableColumn)
      End If
   End Function

   Protected Overrides Function ShouldOutputViewColumnProperty( _
            ByVal propertyName As String, _
            ByVal DbsViewColumn As MetadataWrapper.DbsViewColumn) _
            As Boolean
      Dim isHandled As Boolean = False
      Dim value As Boolean = ShouldOutputTableOrViewColumnProperty(propertyName, DbsViewColumn.NETType, _
               DbsViewColumn.IsAutoIncrement.Value, DbsViewColumn.UseForDesc.Value, DbsViewColumn.Default, isHandled)
      If isHandled Then
         Return value
      Else
         Return MyBase.ShouldOutputViewColumnProperty(propertyName, DbsViewColumn)
      End If
   End Function

   Private Function ShouldOutputTableOrViewColumnProperty( _
            ByVal propertyName As String, _
            ByVal netType As String, _
            ByVal isAutoIncrement As Boolean, _
            ByVal isUseForDesc As Boolean, _
            ByVal defaultValue As String, _
            ByRef isHandled As Boolean) _
            As Boolean
      isHandled = True
      Select Case propertyName.ToLower()
         Case "precision"
            Return (IsTypeNameNumeric(netType) OrElse _
                     IsTypeNameDate(netType))
         Case "scale"
            Return ((IsTypeNameNumeric(netType) OrElse _
                     IsTypeNameDate(netType)) AndAlso Not IsTypeNameApproximate(netType))
         Case "maxlength"
            Return (IsTypeNameString(netType))
         Case "isautoincrement"
            Return (isAutoIncrement)
         Case "usefordesc"
            ' The current rules for this in extract metadata are problematic
            Return (isUseForDesc)
         Case "default"
            ' The current rules for this in extract metadata are problematic
            Return (Not String.IsNullOrEmpty(defaultValue))
      End Select
      isHandled = False
      Return False
   End Function

   Protected Overrides Function ShouldOutputTableProperty( _
            ByVal propertyName As String, _
            ByVal DbsTable As MetadataWrapper.DbsTable) _
            As Boolean
      Select Case propertyName.ToLower
         Case "islookup"
            Return (DbsTable.IsLookup.Value)
         Case "deletepriority"
            Return DbsTable.DeletePriority.Value <> 0
      End Select
      Return MyBase.ShouldOutputTableProperty(propertyName, DbsTable)
   End Function

   Protected Overrides Function ShouldOutputTableChild _
            (Of T) _
            (ByVal childName As String, _
            ByVal child As T, _
            ByVal table As DbsTable) _
            As Boolean
      Dim childCollection As DbsChildTableCollection = TryCast(child, DbsChildTableCollection)
      If childCollection IsNot Nothing Then
         Return childCollection.Count > 0
      End If
      Dim parentCollection As DbsParentTableCollection = TryCast(child, DbsParentTableCollection)
      If parentCollection IsNot Nothing Then
         Return parentCollection.Count > 0
      End If
      Return MyBase.ShouldOutputTableChild(Of T)(childName, child, table)
   End Function

   Protected Overrides Function ShouldOutputTableColumnChild _
            (Of T) _
            (ByVal childName As String, _
            ByVal child As T, _
            ByVal DbsTableColumn As MetadataWrapper.DbsTableColumn) _
            As Boolean
      Dim isHandled As Boolean
      Dim value As Boolean = ShouldOutputTableOrViewColumnChild(childName, child, isHandled)
      If isHandled Then
         Return value
      Else
         Return MyBase.ShouldOutputTableColumnChild(Of T)(childName, child, DbsTableColumn)
      End If
   End Function

   Protected Overrides Function ShouldOutputViewColumnChild _
            (Of T) _
            (ByVal childName As String, _
            ByVal child As T, _
            ByVal DbsViewColumn As MetadataWrapper.DbsViewColumn) _
            As Boolean
      Dim isHandled As Boolean
      Dim value As Boolean = ShouldOutputTableOrViewColumnChild(childName, child, isHandled)
      If isHandled Then
         Return value
      Else
         Return MyBase.ShouldOutputViewColumnChild(Of T)(childName, child, DbsViewColumn)
      End If
   End Function

   Private Function ShouldOutputTableOrViewColumnChild( _
            ByVal childName As String, _
            ByVal child As Object, _
            ByRef isHandled As Boolean) _
            As Boolean
      If childName = "CheckConstraints" Then
         isHandled = True
         Dim constraintCollection As DbsCheckConstraintCollection = TryCast(child, DbsCheckConstraintCollection)
         If constraintCollection IsNot Nothing Then
            Return constraintCollection.Count > 0
         End If
      End If
      Return False
   End Function

   Protected Overrides Function ShouldOutputViewProperty( _
            ByVal propertyName As String, _
            ByVal DbsView As MetadataWrapper.DbsView) _
            As Boolean
      Select Case propertyName.ToLower
         Case "islookup"
            Return False
      End Select
      Return MyBase.ShouldOutputViewProperty(propertyName, DbsView)
   End Function

   Protected Overrides Function ShouldOutputStoredProcProperty( _
            ByVal propertyName As String, _
            ByVal DbsStoredProc As MetadataWrapper.DbsStoredProc) _
            As Boolean
      Select Case propertyName.ToLower
         Case "type"
            Return Not String.IsNullOrEmpty(DbsStoredProc.Type)
      End Select
      Return MyBase.ShouldOutputStoredProcProperty(propertyName, DbsStoredProc)
   End Function

   Protected Overrides Function ShouldOutputStoredProcParameterProperty( _
            ByVal propertyName As String, _
            ByVal DbsStoredProcParameter As MetadataWrapper.DbsStoredProcParameter) _
            As Boolean
      Select Case propertyName.ToLower
         Case "maxlength"
            Return (IsSqlTypeNameString(DbsStoredProcParameter.Type))
      End Select
      Return MyBase.ShouldOutputStoredProcParameterProperty(propertyName, DbsStoredProcParameter)
   End Function

End Class

Public Class DbsCollection
   Inherits genDbsCollection

End Class

